package tour;

import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.tree.ParseTree;
import org.antlr.v4.runtime.tree.ParseTreeWalker;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class InsertSerialID {
    public static void main(String[] args) throws IOException {
        String inputFile = null;
        if (args.length > 0) {
            inputFile = args[0];
        }
        InputStream is = System.in;
        if(inputFile != null) {
            is = new FileInputStream(inputFile);
        }
        CharStream input = CharStreams.fromStream(is);
        JavaLexer lexer = new JavaLexer(input);
        CommonTokenStream tokenStream = new CommonTokenStream(lexer);
        JavaParser parser = new JavaParser(tokenStream);
        // start parsing
        ParseTree tree = parser.compilationUnit();
        ParseTreeWalker walker = new ParseTreeWalker();
        // get listener
        InsertSerialIDListener extractor = new InsertSerialIDListener(tokenStream);
        walker.walk(extractor, tree);

        // print back altered stream
        System.out.println(extractor.rewriter.getText());
    }
}
/* example
 package tour;

 // demo for getting interface from class

 import java.util.List;
 import java.util.Map;
 public class Demo {
 void f(int x, String y) { }
 int[ ] g() { return null; }
        List<Map<String, Integer>>[] h() { return null; }
        }

 **/
// convert to
/*
 package tour;

 // demo for getting interface from class

 import java.util.List;
 import java.util.Map;
 public class Demo {
 public static final long serialVersionUID = 1L;
 void f(int x, String y) { }
 int[ ] g() { return null; }
        List<Map<String, Integer>>[] h() { return null; }
        }
 */